#!/usr/bin/env perl
require 5.008;
use warnings;
use strict;

unshift(@INC, '.');
require qpdf_test_helpers;

chdir("qpdf") or die "chdir testdir failed: $!\n";

require TestDriver;

cleanup();

my $td = new TestDriver('rotate-pages');

my $n_tests = 18;

# Do absolute, positive, and negative on ranges that include
# inherited and non-inherited.
# Pages 11-15 inherit /Rotate 90
# Pages 1 and 2 have explicit /Rotate 270
# Pages 16 and 17 have explicit /Rotate 180

$td->runtest("page rotation",
             {$td->COMMAND => "qpdf --static-id to-rotate.pdf a.pdf" .
                  " --rotate=+90:1,4,11,16" .
                  " --rotate=180:2,5,12-13" .
                  " --rotate=-90:3,15,17,18"},
             {$td->STRING => "", $td->EXIT_STATUS => 0});
$td->runtest("check output",
             {$td->COMMAND => "qpdf-test-compare a.pdf rotated.pdf"},
             {$td->FILE => "rotated.pdf", $td->EXIT_STATUS => 0});

$td->runtest("remove rotation",
             {$td->COMMAND => "qpdf --static-id rotated.pdf a.pdf" .
                  " --qdf --no-original-object-ids --rotate=0"},
             {$td->STRING => "", $td->EXIT_STATUS => 0});
$td->runtest("check output",
             {$td->COMMAND => "qpdf-test-compare a.pdf unrotated.pdf"},
             {$td->FILE => "unrotated.pdf", $td->EXIT_STATUS => 0});

$td->runtest("rotate all pages",
             {$td->COMMAND =>
                  "qpdf --static-id --rotate=180 minimal.pdf a.pdf"},
             {$td->STRING => "", $td->EXIT_STATUS => 0});
$td->runtest("check output",
             {$td->COMMAND => "qpdf-test-compare a.pdf minimal-rotated.pdf"},
             {$td->FILE => "minimal-rotated.pdf", $td->EXIT_STATUS => 0});

$td->runtest("flatten with inherited rotate",
             {$td->COMMAND =>
                  "qpdf --static-id --flatten-rotation" .
                  " inherited-rotate.pdf a.pdf"},
             {$td->STRING => "", $td->EXIT_STATUS => 0});
$td->runtest("check output",
             {$td->COMMAND => "qpdf-test-compare a.pdf inherited-flattened.pdf"},
             {$td->FILE => "inherited-flattened.pdf", $td->EXIT_STATUS => 0});

foreach my $angle (qw(90 180 270))
{
    $td->runtest("rotate annotations",
                 {$td->COMMAND =>
                      "qpdf --static-id --qdf --rotate=$angle" .
                      " --flatten-rotation --no-original-object-ids" .
                      " form-fields-and-annotations.pdf a.pdf"},
                 {$td->STRING => "", $td->EXIT_STATUS => 0});
    $td->runtest("check output (flatten $angle)",
                 {$td->FILE => "a.pdf"},
                 {$td->FILE => "annotations-rotated-$angle.pdf"});
}

# The file form-fields-and-annotations-shared.pdf contains some
# annotations that appear in multiple pages /Annots, some non-shared
# things that share appearance streams, some form fields appear on
# multiple pages, and an indirect /Annotations array. It is out of
# spec in several ways but still works in most viewers. These test
# make sure we don't make anything worse and also end up exercising
# some cases of things being copied more than once, though we also
# exercise that with legitimate test cases using overlay.

$td->runtest("shared annotations 1 page",
             {$td->COMMAND =>
                  "qpdf --qdf --no-original-object-ids --static-id" .
                  " --rotate=90:1 form-fields-and-annotations-shared.pdf" .
                  " a.pdf --flatten-rotation"},
             {$td->STRING => "", $td->EXIT_STATUS => 0},
             $td->NORMALIZE_NEWLINES);
$td->runtest("check output",
             {$td->FILE => "a.pdf"},
             {$td->FILE => "rotated-shared-annotations-1.pdf"});
$td->runtest("shared annotations 2 pages",
             {$td->COMMAND =>
                  "qpdf --qdf --no-original-object-ids --static-id" .
                  " --rotate=90:1,2 form-fields-and-annotations-shared.pdf" .
                  " a.pdf --flatten-rotation"},
             {$td->STRING => "", $td->EXIT_STATUS => 0},
             $td->NORMALIZE_NEWLINES);
$td->runtest("check output",
             {$td->FILE => "a.pdf"},
             {$td->FILE => "rotated-shared-annotations-2.pdf"});

cleanup();
$td->report($n_tests);
